import React, { Component } from 'react';
import './Myheader.scss'
import { withRouter } from 'react-router-dom'
import events from '../../utils/events'
class Myheader extends Component {
    constructor(props){
        super(props)
        this.state={
            val:''
        }
    }
    handleChange(e){
        this.setState({
            val:e.target.type == 'checkbox' || e.target.type == "arido" ? e.target.checked : e.target.value
        },()=>{
            console.log(this.state.val);
        }
        )
    }
    handleKeyDown(){
        if(window.event.keyCode === 13){
            // 获取当前页的路由
            let pathnames = this.props.history.location.pathname
            // console.log(pathnames);
            // console.log(this.state.val);
            if(this.state.val){
                // 对路由进行判断，是那个页面触发的事件
                if(pathnames == '/index/classify'){
                    this.props.history.push({pathname:"/index/allgood",state:{searchkey:this.state.val,}})
                }
                else if(pathnames == '/index/allgood'){
                    // 兄弟传值
                    events.emit('searchkey',this.state.val)
                }
            }

        }
    }
    render() {
        return (
            <div className='myheader'>
                <h2>{this.props.title}</h2>
                <div className='inputBox'>
                    <i className=' iconfont icon-fangdajing'></i>
                    <input type="text" value={this.state.val} onChange={(e)=>{this.handleChange(e)}}placeholder='搜索店铺内商品' onKeyDown={()=>{this.handleKeyDown()}}/>
                </div>
            </div>
        );
    }
}

export default withRouter(Myheader);